﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Microsoft.ApplicationBlocks.Data;

using YouHP.IDAL;
using YouHP.Model;

namespace YouHP.SqlServerDAL
{
    public class SymptomDAL : ISymptom
    {
        #region 症状
        public SymptomInfo GetSymptomInfo(int id)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.GetFullName(Common.SymptomSP_GetSymptom),
                id) as IDataReader;
            return CBO.FillObject<SymptomInfo>(reader);
        }

        public bool AddSymptom(SymptomInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GetFullName(Common.SymptomSP_AddSymptom),
                info.Name, info.PinYin, info.Type,
                info.MildDescription,
                info.ModerateDescription,
                info.SevereDescription,
                info.Intro, info.Desp,
                info.UpdateTime,
                info.PatientId,
                info.PatientCount
                ) > 0;
        }

        public bool UpdateSymptom(SymptomInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GetFullName(Common.SymptomSP_UpdateSymptom),
                info.Id, info.Name, info.PinYin, info.Type,
                info.MildDescription,
                info.ModerateDescription,
                info.SevereDescription,
                info.Intro,
                info.Desp,
                info.UpdateTime,
                info.PatientId,
                info.PatientName
                ) > 0;
        }

        public bool DeleteSymptom(int[] itemIds)
        {
            int cnt = itemIds.Length;
            if (cnt <= 0) return false;
            StringBuilder sb = new StringBuilder();
            sb.Append("[id] in (");
            sb.Append(itemIds[0]);

            for (int i = 1; i < cnt; i++)
            {
                sb.Append(", ");
                sb.Append(itemIds[i]);
            }
            sb.Append(")");

            int cnt2 = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GetFullName(Common.SymptomSP_DelSymptom),
                sb.ToString());

            return (cnt2 == cnt);
        }
        #endregion

        #region 症状检索与列表
        private  List<SymptomInfo> GetSymptomList(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                 Common.SymptomSP_GetSymptomList,
                 pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<SymptomInfo>(dr);
        }

        private List<SymptomInfo> GetSymptomListWithPatientInfo(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                 Common.SymptomSP_GetSymptomListWithPatientInfo,
                 pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<SymptomInfo>(dr);
        }

        private List<SymptomInfo> GetSymptomListByDiseaseWithPatientInfo(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                 Common.SymptomSP_GetSymptomListByDiseaseWithPatientInfo,
                 pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<SymptomInfo>(dr);
        }

        private List<SymptomInfo> GetSymptomListWithDiseaseInfo(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                 Common.SymptomSP_GetSymptomListByDisease,
                 pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<SymptomInfo>(dr);
        }

        private List<SymptomInfo> GetSymptomListByDiseaseWithDiseaseInfo(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                 Common.SymptomSP_GetSymptomListByDiseaseWithDiseaseInfo,
                 pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<SymptomInfo>(dr);
        }

        /*按条件检索症状列表*/
        public List<SymptomInfo> GetSymptomList(int pageSize, int pageIndex, string condStr, out int count)
        {
            return GetSymptomList(pageSize, pageIndex, condStr, " [Symptom].[PatientCount] desc ", out count);
        }
        public List<SymptomInfo> GetSymptomListWithOrder(int pageSize, int pageIndex, string condStr,string sOrder, out int count)
        {
            return GetSymptomList(pageSize, pageIndex, condStr, sOrder, out count);
        }

        /*按条件检索症状列表（包括典型疾病，典型病人信息）*/
        public List<SymptomInfo> GetSymptomListWithDiseaseInfo(int pageSize, int pageIndex, string condStr, out int count)
        {
            return GetSymptomListWithDiseaseInfo(pageSize, pageIndex, condStr, " [Symptom].[PatientCount] desc ", out count);
        }

        /*按疾病条件检索症状列表（包括典型疾病，典型病人信息）*/
        public List<SymptomInfo> GetSymptomListByDiseaseWithDiseaseInfo(int pageSize, int pageIndex, string condStr, out int count)
        {
            return GetSymptomListByDiseaseWithDiseaseInfo(pageSize, pageIndex, condStr, " [Symptom].[PatientCount] desc ", out count);
        }

        /*按条件检索症状列表（包括典型病人信息）*/
        public List<SymptomInfo> GetSymptomListWithPatientInfo(int pageSize, int pageIndex, string condStr, out int count)
        {
            return GetSymptomListWithPatientInfo(pageSize, pageIndex, condStr, " [Symptom].[PatientCount] desc ", out count);
        }

        /*按疾病条件检索症状列表（包括典型病人信息）*/
        public List<SymptomInfo> GetSymptomListByDiseaseWithPatientInfo(int pageSize, int pageIndex, string condStr, out int count)
        {
            return GetSymptomListByDiseaseWithPatientInfo(pageSize, pageIndex, condStr, " [Symptom].[PatientCount] desc ", out count);
        }

        /*按照症状名称来检索症状*/
        public List<SymptomInfo> GetSymptomByName(int pageSize, int pageIndex, string keyword, out int count)
        {
            string condstr = string.Format(@" [symptomname] like '%{0}%' ", keyword);
            return GetSymptomList(pageSize, pageIndex, condstr, out count);
        }

        /*按照症状名称拼音字母来检索症状*/
        public List<SymptomInfo> GetSymptomByPinYin(int pageSize, int pageIndex, string pinYin, out int count)
        {
            string condstr = string.Format(@" [firstsymbolofname] like '%{0}%' ", pinYin.ToUpper());
            return GetSymptomList(pageSize, pageIndex, condstr, out count);
        }

        /*按照症状名称来检索症状*/
        public List<SymptomInfo> GetSymptomByType(int pageSize, int pageIndex, int symptomTypeId, out int count)
        {
            string condstr = string.Format(@" [symptomtypeid]={0} ", symptomTypeId);
            return GetSymptomList(pageSize, pageIndex, condstr, out count);
        }
        #endregion

        #region 症状类型
        public bool AddSymptomType(SymptomTypeInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GetFullName(Common.SymptomSP_AddSymptomType),
                info.Name, info.ParentId);
            return (cnt == 1);
        }

        public bool DelSymptomType(int[] itemIds)
        {
            int cnt = itemIds.Length;
            if (cnt <= 0)
                return false;

            StringBuilder sb = new StringBuilder();
            sb.Append("[id] in (");
            sb.Append(itemIds[0]);

            for (int i = 1; i < cnt; i++)
            {
                sb.Append(", ");
                sb.Append(itemIds[i]);
            }
            sb.Append(")");

            int cnt2 = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.SymptomSP_DelSymptomType,
                sb.ToString());
            return cnt2 >= cnt;
        }

        public bool UpdateSymptomType(SymptomTypeInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GetFullName(Common.SymptomSP_UpdateSymptomType),
                info.Id, info.Name, info.ParentId);
            return (cnt > 0);
        }

        public SymptomTypeInfo GetSymptomTypeInfo(int typeId)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.GetFullName(Common.SymptomSP_GetSymptomType),
                typeId);
            return CBO.FillObject<SymptomTypeInfo>(reader);
        }

        public List<SymptomTypeInfo> GetSymptomTypeList(int parentId)
        {
            string condstr = string.Format(" [parentid]={0} ", parentId);
            return GetSymptomTypeList(condstr);
        }

        public List<SymptomTypeInfo> GetSymptomTypeList()
        {
            return GetSymptomTypeList("");
        }

        private List<SymptomTypeInfo> GetSymptomTypeList(string condstr)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.GetFullName(Common.SymptomSP_GetSymptomTypeList),
                condstr);
            return CBO.FillCollection<SymptomTypeInfo>(reader);
        }
        #endregion

        #region 症状统计
        public List<SymptomStatDiseaseCount> GetSymptomStatDisease(int symId)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.SymptomSP_GetSymptomStatDisease,
                symId ) as IDataReader;
            return CBO.FillCollection<SymptomStatDiseaseCount>(reader);
        }

        public List<SymptomStatSeverityCount> GetSymptomStatSeverity(int symId)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.SymptomSP_GetSymptomStatSeverity,
                symId) as IDataReader;
            return CBO.FillCollection<SymptomStatSeverityCount>(reader);
        }
        #endregion
    }
}
